package com.css.apps.base.log.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.log.model.ClientOsInfo;
import com.css.apps.base.log.service.QueryLog;
import com.css.db.page.Page;
import com.css.db.query.QueryCache;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;

import net.sf.json.JSONObject;

@SuppressWarnings("serial")
public class DirClientOfOsLoginLog extends AdminAction {

	private static Log log = LogFactory.getLog(DirClientOfExLog.class);

	private String countType;
	private String logType;
	private String countKey;
	private Page page;
	private String whetherQuery;

	public DirClientOfOsLoginLog() {
		page = new Page();
		page.setPageSize(16);
	}

	@SuppressWarnings("unchecked")
	@Override
	protected String adminGo() {
		if (whetherQuery == null || "".equals(whetherQuery)) {
			return Action.SUCCESS;
		} else {
			try {
				StringBuilder sb = new StringBuilder("select a.uuid ,a.content, a.countKey from SLogClientinfo a "
						+ getWhere() + " order by a.countKey ");

				QueryLog qc = new QueryLog(sb.toString());
				setWhere(qc);
				page.setCountField(" distinct a.uuid ");
				page = qc.pageCache(page);
				page.setResults(getToDoObject(page.getResults()));
				return Action.SUCCESS;
			} catch (Exception ex) {
				log.error(ex.getMessage(), ex);
				setMessage(Messages.getString("systemMsg.exception"));
				return Action.ERROR;
			}
		}
	}

	public String getWhere() {
		StringBuffer sb = new StringBuffer(" where 1=1 ");
		sb.append("and a.countType = :countType ");
		if (StringHelper.isNotEmpty(logType))
			sb.append(" and a.logType = :logType ");
		if (StringHelper.isNotEmpty(countKey)) {
			sb.append(" and a.countKey between :beginDate and :endDate ");
		}
		return sb.toString();
	}

	public void setWhere(QueryCache qc) {
		// 4代表统计类型为登录操作系统
		qc.setParameter("countType", "4");
		if (StringHelper.isNotEmpty(logType))
			qc.setParameter("logType", logType);
		if (StringHelper.isNotEmpty(countKey)) {
			String[] date = getBeginAndEndDate(countKey);
			String beginDate = date[0].trim();
			String endDate = date[1].trim();
			qc.setParameter("beginDate", beginDate);
			qc.setParameter("endDate", endDate);
		}

	}

	@SuppressWarnings("unchecked")
	private List<Object> getToDoObject(List<Object[]> contents) throws Exception {
		List<Object> osLoginList = new ArrayList<Object>();
		ClientOsInfo exinfo = new ClientOsInfo();
		// JSONArray array = JSONArray.fromObject(contents);
		// array.get(0);
		if (contents != null && contents.size() > 0) {
			// VO对象处理
			for (int i = 0; i < contents.size(); i++) {
				exinfo = new ClientOsInfo();
				Object[] obj = contents.get(i);
				exinfo.setDate(obj[2].toString());// set日期
				JSONObject jsonObject = JSONObject.fromObject(obj[1]);
				List<Object> ex = jsonObject.names();
				System.out.println("");
				// 1,2,3,4分别代表不同的操作系统
				for (int j = 0; j < ex.size(); j++) {
					if (ex.get(j).equals("1")) {
						exinfo.setWindows(jsonObject.getString(ex.get(j).toString()));
					} else if (ex.get(j).equals("2")) {
						exinfo.setLinuxs(jsonObject.getString(ex.get(j).toString()));
					} else if (ex.get(j).equals("3")) {
						exinfo.setAndriod(jsonObject.getString(ex.get(j).toString()));
					} else if (ex.get(j).equals("4")) {
						exinfo.setIos(jsonObject.getString(ex.get(j).toString()));
					} else if (ex.get(j).equals("0")) {
						exinfo.setOther(jsonObject.getString(ex.get(j).toString()));
					}
				}
				osLoginList.add(exinfo);
			}

			return osLoginList;
		}
		return osLoginList;
	}

	private String[] getBeginAndEndDate(String countKey) {
		String[] result = new String[2];
		String[] date = countKey.split(",");
		int index = (Integer.parseInt(logType) - 1) * 2;
		result[0] = date[index];
		result[1] = date[index + 1];
		return result;
	}

	public String getCountType() {
		return countType;
	}

	public void setCountType(String countType) {
		this.countType = countType;
	}

	public String getLogType() {
		return logType;
	}

	public void setLogType(String logType) {
		this.logType = logType;
	}

	public String getCountKey() {
		return countKey;
	}

	public void setCountKey(String countKey) {
		this.countKey = countKey;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String getWhetherQuery() {
		return whetherQuery;
	}

	public void setWhetherQuery(String whetherQuery) {
		this.whetherQuery = whetherQuery;
	}

}
